﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Reflection;
using Yes.Domain;
using Yes.Domain.Entity;


namespace Yes.Repository
{
    public class YesDbContext :  DbContext, IDbContext
    {

        public YesDbContext(DbContextOptions<YesDbContext> options) : base(options)
        {
            
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 动态地加入实体
            var typesToRegister = Assembly.GetAssembly(typeof(EntityBase)).GetTypes().Where(type => !String.IsNullOrEmpty(type.Namespace)).Where(type => type.BaseType == typeof(EntityBase));
            foreach (var entityType in typesToRegister)
            {
                if (modelBuilder.Model.FindEntityType(entityType) != null)
                {
                    continue;
                }
                modelBuilder.Model.AddEntityType(entityType);
            }
        }

        
        /*
        public new DbSet<TEntity> Set<TEntity>() where TEntity : class
        {
            return base.Set<TEntity>();
        }
         */


    }
}
